package search_simpleHTMLlist;

import java.io.*;

import jxl.JXLException;

import benchmarking.benchmarker;

public class SearchCmd {
    public static void main(String[] args) throws IOException, JXLException {
        String name;

        // Check that a filename has been given as argument
        if (args.length != 1) {															/*1*/
            System.out.println("Usage: java SearchCmd <datafile>");
            System.exit(1);
        }

        // Read the file and create the linked list
        HTMLlist l = Searcher.readHtmlList (args[0]);

        if (l.str == null) {															/*1*/
            System.out.println("Usage: java SearchCmd <datafile> is empty");
            System.exit(1);
        }
        // Ask for a word to search
        BufferedReader inuser =
            new BufferedReader (new InputStreamReader (System.in));

        System.out.println ("Hit return to exit.");
        boolean quit = false;
        String URLsFound = "";
        
        //Creating a benchmark timer and as sheet for results
        benchmarker timer = new benchmarker();
        timer.createSheet("large");
        int i =1;
        
        while (!quit) {																	
      /*The user input is removed for benchmarking
       * Instead we benchmark with the word "test" a hundred times 
       
        	System.out.print ("Search for: ");
            name = inuser.readLine(); // Read a line from the terminal
            */
        	
        	//Benchmark stuff
        	name = "alfabetmandenik�benhavn";
        	i++;
        	if (i==100) name="";
            timer.start();
            
            
            
            
            if (name == null || name.length() == 0) {													/*2*/
                quit = true;
            }
            // Store the return value in 'URLsFound' and if this variable
            // is not empty, we have found one or more URLs where 'name'
            // exists, so print the result to the console window            
            else if (!((URLsFound = Searcher.existsWithURLs(l, name)).isEmpty()))						/*3*/
            {
            	timer.end();
            	timer.toExel("test");
                System.out.println("The word \"" + name + "\" has been found in the following URLs:");
                System.out.println(URLsFound);
            } else {																					/*4*/
            	timer.end();
            	timer.toExel("test");
                System.out.println("The word \"" + name + "\" has not been found.");
            }
            
            System.out.println("The search was completed in "
            +timer.toString()+" nanoseconds.");
            
            
        }
        timer.close();
    }
}